package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.GoodsApi;
import com.kgc.sbt.domain.Goods;
import com.kgc.sbt.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class GoodsProvider implements GoodsApi {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Goods queryGoodsById(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean putGoodsById(Integer id) {
        Goods goods=goodsMapper.selectByPrimaryKey(id);
        if(goods.getStatus()==1){
            System.out.println("已上架，无需再次上架");
            return false;
        }
      goodsMapper.updateGoodsStatus(id);
        System.out.println("上架成功");
goodsMapper.updateLaunchTime(id);
        return  true;
    }

    @Override
    public boolean pullGoodsById(Integer id) {
        Goods goods=goodsMapper.selectByPrimaryKey(id);
        if(goods.getStatus()==0){
//            System.out.println("已下架，无需再次下架");
            return false;
        }
        goodsMapper.updateGoodsStatus1(id);
        System.out.println("下架成功");


        return  true;
    }

    @Override
    public boolean putStockById(Integer id,Integer stockNumber) {
        goodsMapper.updateGoodsStock(id, stockNumber);
        goodsMapper.updateTime(id);
        System.out.println("增加库存成功");
        return true;
    }

    @Override
    public boolean pullStockById(Integer id, Integer stockNumber) {
        goodsMapper.updateGoodStock1(id, stockNumber);
        Goods goods=goodsMapper.selectByPrimaryKey(id);
        if (goods.getStock()<0){
           goodsMapper.updateGoodStock2(id, stockNumber);
           System.out.println("库存小于0，最多减少到0");
           goodsMapper.updateTime(id);
            System.out.println("修改时间改为当前时间，");
           return false;
        }
        goodsMapper.updateTime(id);
        System.out.println("删除库存成功");
        System.out.println("修改时间改为当前时间。");

        return true;
    }


}